/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompetenceAnalyzerMainFrame.java
 *
 * Created on May 12, 2010, 3:42:42 PM
 */

package at.tugraz.kmi.css.analyzer.gui;

import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.logging.Logger;

import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import net.guts.gui.action.GutsAction;
import net.guts.gui.application.GutsApplicationActions;
import net.guts.gui.dialog.DialogFactory;
import net.guts.gui.menu.MenuFactory;
import at.tugraz.kmi.css.analyzer.model.CompetenceAnalyzerModel;
import at.tugraz.kmi.css.analyzer.model.StructureModel;
import at.tugraz.kmi.css.cbkst2.prereq.structurevis.CompetenceManagerModel;
import at.tugraz.kmi.css.cbkst2.prereq.structurevis.CompetenceManagerView;
import at.tugraz.kmi.css.commons.MessageFormatFormatter;
import at.tugraz.kmi.css.commons.TextAreaLogHandler;

import com.google.inject.Inject;
import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.beans.PropertyConnector;
import net.guts.gui.window.JDialogConfig;

/**
 * 
 * The Main Frame of the GUI.
 * 
 * @author Georg Öttl <georg.oettl@gmail.com>
 */
public class CompetenceAnalyzerMainFrame extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long			serialVersionUID	= 1L;

	private GutsApplicationActions		gutsAppActions;

	private StructureModel				structureActions;

	private CompetenceAnalyzerModel		camModel;

	private transient PropertyConnector	connector1;

	private transient PropertyConnector	connector4;

	private transient GutsAction		aboutAction;

	private PropertyConnector			connector2;

	private PropertyConnector			connector3;

	public CompetenceAnalyzerMainFrame() {
		initComponents();
		Logger logger = Logger.getLogger("");
		logger.addHandler(new TextAreaLogHandler(jTextAreaLog,
				new MessageFormatFormatter()));
	}

	@Inject
	CompetenceAnalyzerMainFrame(StructureModel structureActions,
			CompetenceAnalyzerModel mainActions,
			GutsApplicationActions gutsAppActions, MenuFactory menuFactory,
			CompetenceAnalyzerMainPanel jMainPanel,
			CompetenceManagerView structureViewer,
			CompetenceAnalyzerModel camModel, final DialogFactory dialogFactory) {
		this();
		this.camModel = camModel;
		this.gutsAppActions = gutsAppActions;
		this.structureActions = structureActions;
		aboutAction = new GutsAction("about") {
			@Override
			protected void perform() {
				dialogFactory.showDialog(new AboutPanel(),  JDialogConfig.create().config());
			}
		};

		addComponentToPane(jMainPanel);
		addComponentToPane(structureViewer);
		initModelActions();
		initConnectors();
		jCheckBoxMenuItemClosureView.setVisible(false);
		jScrollPane1.setVisible(false);
	}

	void initConnectors() {
		Bindings.bind(
				jScrollPane1,
				"visible",
				new BeanAdapter<CompetenceAnalyzerModel>(camModel)
						.getValueModel(CompetenceAnalyzerModel.PROP_LOG_ENABLED));

		connector1 = PropertyConnector.connect(camModel,
				CompetenceAnalyzerModel.PROP_LOG_ENABLED,
				jCheckBoxMenuItemEnableLog, "selected");
		connector1.updateProperty1();
		connector2 = PropertyConnector.connect(camModel,
				CompetenceAnalyzerModel.PROP_LOG_ENABLED, jScrollPane1,
				"visible");
		connector2.updateProperty2();
		connector3 = PropertyConnector.connect(
				camModel.getPrerequesitesModel(),
				CompetenceManagerModel.PROPERTY_SHOW_TRANSITIVE_CLOSURE_VIEW,
				jMenuStructure, "visible");
		connector3.updateProperty2();

		connector4 = PropertyConnector.connect(camModel,
				CompetenceAnalyzerModel.PROP_STRUCTURE_VIEW_ENABLED,
				jMenuStructure, "visible");
		connector4.updateProperty2();
	}

	public void addComponentToPane(JPanel panel) {
		// Put the JComboBox in a JPanel to get a nicer look.
		jComboBox1.addItem(panel.getName());
		jComboBox1.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent evt) {
				CardLayout cl = (CardLayout) (jPanelMain.getLayout());
				String name = (String) evt.getItem();
				cl.show(jPanelMain, name);
				if (name.contains("CompetenceStructureEditor")) {
					camModel.setStructureViewEnabled(true);
				} else {
					camModel.setStructureViewEnabled(false);
				}
			}
		});

		jPanelMain.add(panel, panel.getName());
	}

	private <T extends JComponent> void initMenuItem(JMenu menu,
			JMenuItem item, GutsAction action) {
		// item.setName(menu.getName() + "-" +
		// at.tugraz.kmi.css.analyzer.name());
		item.setAction(action);
		menu.add(item);
	}

	private void initModelActions() {
		initMenuItem(jMenuEdit, jMenuItemCut, gutsAppActions.cut());
		initMenuItem(jMenuEdit, jMenuItemCopy, gutsAppActions.copy());
		initMenuItem(jMenuEdit, jMenuItemPaste, gutsAppActions.paste());
		initMenuItem(jMenuFile, jMenuItemQuit, gutsAppActions.quit());
		initMenuItem(jMenuHelp, jMenuItemAbout, aboutAction);
		initMenuItem(jMenuStructure, jMenuItemNewStructure,
				structureActions.getNewStructureAction());
		initMenuItem(jMenuStructure, jMenuItemLoadStructure,
				structureActions.getLoadStructureAction());
		initMenuItem(jMenuStructure, jMenuItemSaveStructure,
				structureActions.getSaveStructureAction());
		initMenuItem(jMenuStructure, jMenuItemICB,
				structureActions.getLoadICBAction());
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jInternalFrame1 = new javax.swing.JInternalFrame();
		jPanelStatusMessages = new javax.swing.JPanel();
		jComboBox1 = new javax.swing.JComboBox();
		jPanelMain = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextAreaLog = new javax.swing.JTextArea();
		jMenuBar = new javax.swing.JMenuBar();
		jMenuFile = new javax.swing.JMenu();
		jMenuItemQuit = new javax.swing.JMenuItem();
		jMenuEdit = new javax.swing.JMenu();
		jMenuItemCut = new javax.swing.JMenuItem();
		jMenuItemCopy = new javax.swing.JMenuItem();
		jMenuItemPaste = new javax.swing.JMenuItem();
		jMenuStructure = new javax.swing.JMenu();
		jMenuItemNewStructure = new javax.swing.JMenuItem();
		jMenuItemLoadStructure = new javax.swing.JMenuItem();
		jMenuItemSaveStructure = new javax.swing.JMenuItem();
		jMenuItemICB = new javax.swing.JMenuItem();
		jCheckBoxMenuItemClosureView = new javax.swing.JCheckBoxMenuItem();
		jMenuHelp = new javax.swing.JMenu();
		jMenuItemAbout = new javax.swing.JMenuItem();
		jCheckBoxMenuItemEnableLog = new javax.swing.JCheckBoxMenuItem();

		jInternalFrame1.setVisible(true);

		javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(
				jInternalFrame1.getContentPane());
		jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
		jInternalFrame1Layout.setHorizontalGroup(jInternalFrame1Layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 0, Short.MAX_VALUE));
		jInternalFrame1Layout.setVerticalGroup(jInternalFrame1Layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 0, Short.MAX_VALUE));

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setMinimumSize(new java.awt.Dimension(800, 700));
		setResizable(false);

		jPanelStatusMessages.setBackground(new java.awt.Color(255, 128, 0));

		javax.swing.GroupLayout jPanelStatusMessagesLayout = new javax.swing.GroupLayout(
				jPanelStatusMessages);
		jPanelStatusMessages.setLayout(jPanelStatusMessagesLayout);
		jPanelStatusMessagesLayout
				.setHorizontalGroup(jPanelStatusMessagesLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanelStatusMessagesLayout
										.createSequentialGroup()
										.addComponent(
												jComboBox1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(759, Short.MAX_VALUE)));
		jPanelStatusMessagesLayout.setVerticalGroup(jPanelStatusMessagesLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jComboBox1,
						javax.swing.GroupLayout.PREFERRED_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.PREFERRED_SIZE));

		getContentPane().add(jPanelStatusMessages, java.awt.BorderLayout.NORTH);

		jPanelMain.setBackground(new java.awt.Color(254, 254, 254));
		jPanelMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jPanelMain
				.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		jPanelMain.setLayout(new java.awt.CardLayout());
		getContentPane().add(jPanelMain, java.awt.BorderLayout.CENTER);

		jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jScrollPane1
				.addComponentListener(new java.awt.event.ComponentAdapter() {
					@Override
					public void componentHidden(
							java.awt.event.ComponentEvent evt) {
						jScrollPane1ComponentHidden(evt);
					}

					@Override
					public void componentShown(java.awt.event.ComponentEvent evt) {
						jScrollPane1ComponentShown(evt);
					}
				});

		jTextAreaLog.setColumns(20);
		jTextAreaLog.setLineWrap(true);
		jTextAreaLog.setRows(5);
		jScrollPane1.setViewportView(jTextAreaLog);

		getContentPane().add(jScrollPane1, java.awt.BorderLayout.SOUTH);

		jMenuBar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		jMenuBar.setFocusCycleRoot(true);

		jMenuFile.setText("File");
		jMenuFile.setName("fileMenu"); // NOI18N

		jMenuItemQuit.setText("Quit");
		jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItemQuitActionPerformed(evt);
			}
		});
		jMenuFile.add(jMenuItemQuit);

		jMenuBar.add(jMenuFile);

		jMenuEdit.setText("Edit");
		jMenuEdit.setName("editMenu"); // NOI18N

		jMenuItemCut.setText("Cut");
		jMenuItemCut.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItemCutActionPerformed(evt);
			}
		});
		jMenuEdit.add(jMenuItemCut);

		jMenuItemCopy.setText("Copy");
		jMenuEdit.add(jMenuItemCopy);

		jMenuItemPaste.setText("Paste");
		jMenuEdit.add(jMenuItemPaste);

		jMenuBar.add(jMenuEdit);

		jMenuStructure.setText("Structure");
		jMenuStructure.setName("structureMenu"); // NOI18N

		jMenuItemNewStructure.setText("New Structure");
		jMenuStructure.add(jMenuItemNewStructure);

		jMenuItemLoadStructure.setText("Load Structure");
		jMenuStructure.add(jMenuItemLoadStructure);

		jMenuItemSaveStructure.setText("Save Structure");
		jMenuStructure.add(jMenuItemSaveStructure);

		jMenuItemICB.setText("Load ICB");
		jMenuStructure.add(jMenuItemICB);

		jCheckBoxMenuItemClosureView.setSelected(true);
		jCheckBoxMenuItemClosureView.setText("ClosureView");
		jCheckBoxMenuItemClosureView.setName("itemClosure"); // NOI18N
		jCheckBoxMenuItemClosureView
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jCheckBoxMenuItemClosureViewActionPerformed(evt);
					}
				});
		jMenuStructure.add(jCheckBoxMenuItemClosureView);

		jMenuBar.add(jMenuStructure);

		jMenuHelp.setText("Help");
		jMenuHelp.setName("helpMenu"); // NOI18N

		jMenuItemAbout.setText("About");
		jMenuHelp.add(jMenuItemAbout);

		jCheckBoxMenuItemEnableLog.setSelected(true);
		jCheckBoxMenuItemEnableLog.setText("Dev-Log");
		jCheckBoxMenuItemEnableLog.setName("itemEnableLog"); // NOI18N
		jCheckBoxMenuItemEnableLog
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jCheckBoxMenuItemEnableLogActionPerformed(evt);
					}
				});
		jMenuHelp.add(jCheckBoxMenuItemEnableLog);

		jMenuBar.add(jMenuHelp);

		setJMenuBar(jMenuBar);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jCheckBoxMenuItemClosureViewActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jCheckBoxMenuItemClosureViewActionPerformed
		// JCheckBoxMenuItem o = (JCheckBoxMenuItem) evt.getSource();
		// camModel.getPrerequesitesModel().setShowTransitiveClosureView(
		// o.isSelected());
	}// GEN-LAST:event_jCheckBoxMenuItemClosureViewActionPerformed

	private void jScrollPane1ComponentHidden(java.awt.event.ComponentEvent evt) {// GEN-FIRST:event_jScrollPane1ComponentHidden
		jPanelMain.revalidate();
	}// GEN-LAST:event_jScrollPane1ComponentHidden

	private void jScrollPane1ComponentShown(java.awt.event.ComponentEvent evt) {// GEN-FIRST:event_jScrollPane1ComponentShown
		jPanelMain.revalidate();
	}// GEN-LAST:event_jScrollPane1ComponentShown

	private void jCheckBoxMenuItemEnableLogActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jCheckBoxMenuItemEnableLogActionPerformed
		// JCheckBoxMenuItem o = (JCheckBoxMenuItem) evt.getSource();
		// camModel.setLogEnabled(o.isSelected());
	}// GEN-LAST:event_jCheckBoxMenuItemEnableLogActionPerformed

	private void jMenuItemCutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItemCutActionPerformed

	}// GEN-LAST:event_jMenuItemCutActionPerformed

	private void jMenuItemQuitActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItemQuitActionPerformed

	}// GEN-LAST:event_jMenuItemQuitActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new CompetenceAnalyzerMainFrame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JCheckBoxMenuItem	jCheckBoxMenuItemClosureView;
	private javax.swing.JCheckBoxMenuItem	jCheckBoxMenuItemEnableLog;
	private javax.swing.JComboBox			jComboBox1;
	private javax.swing.JInternalFrame		jInternalFrame1;
	private javax.swing.JMenuBar			jMenuBar;
	private javax.swing.JMenu				jMenuEdit;
	private javax.swing.JMenu				jMenuFile;
	private javax.swing.JMenu				jMenuHelp;
	private javax.swing.JMenuItem			jMenuItemAbout;
	private javax.swing.JMenuItem			jMenuItemCopy;
	private javax.swing.JMenuItem			jMenuItemCut;
	private javax.swing.JMenuItem			jMenuItemICB;
	private javax.swing.JMenuItem			jMenuItemLoadStructure;
	private javax.swing.JMenuItem			jMenuItemNewStructure;
	private javax.swing.JMenuItem			jMenuItemPaste;
	private javax.swing.JMenuItem			jMenuItemQuit;
	private javax.swing.JMenuItem			jMenuItemSaveStructure;
	private javax.swing.JMenu				jMenuStructure;
	private javax.swing.JPanel				jPanelMain;
	private javax.swing.JPanel				jPanelStatusMessages;
	private javax.swing.JScrollPane			jScrollPane1;
	private javax.swing.JTextArea			jTextAreaLog;
	// End of variables declaration//GEN-END:variables

}
